import { createRouter, createWebHistory } from 'vue-router'
import { LoadingBar } from '@varlet/ui'



const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login', name: 'login', component: () => import('../views/login/LoginPage.vue'),
    },
    { path: '/home', name: 'home', component: () => import('../views/home/HomeView.vue') },
    { path: '/user', name: 'user', component: () => import('../views/user/UserView.vue') },
    { path: '/cart', name: 'cart', component: () => import('../views/cart/CartView.vue') },

    { path: '/comemnt', name: 'comemnt', component: () => import('../views/sort/CommentView.vue') },

    { path: '/sort', name: 'sort', component: () => import('../views/sort/SortView.vue') },
    {
      path: '/find', name: 'find', component: () => import('../views/find/FindView.vue'),
      children: [
        {path: '', name:'find',component: () => import('../views/find/findItem/FindAttentionView.vue')},
        { path: 'recommend', name: 'recommend', component: () => import('../views/find/findItem/FindRecommendView.vue') },
        { path: 'detail', name: 'detail', component: () => import('../views/find/findItem/recommendItemContent/DetailItemContent.vue') },
        { path: 'article', name: 'article', component: () => import('../views/find/findItem/recommendItemContent/ArticleItemContent.vue') },
        { path: 'test', name: 'test', component: () => import('../views/find/findItem/recommendItemContent/TestItemContent.vue') },
        { path: 'question', name: 'question', component: () => import('../views/find/findItem/recommendItemContent/QuestionItemContent.vue') },
      ]

    },

    {
      path: '/micircle', name: 'micircle', component: () => import('../views/find/micircle/MicircleView.vue'),
      redirect: '/micircle/rec_list'},
    // { path: '/find/detail', name: 'detail', component: () => import('../views/find/findItem/recommendItemContent/DetailItemContent.vue') },
    // { path: '/find/article', name: 'article', component: () => import('../views/find/findItem/recommendItemContent/ArticleItemContent.vue') },
    // { path: '/find/test', name: 'test', component: () => import('../views/find/findItem/recommendItemContent/TestItemContent.vue') },
    // { path: '/find/question', name: 'question', component: () => import('../views/find/findItem/recommendItemContent/QuestionItemContent.vue') },
    {
      path: '/micircle', name: 'micircle', component: () => import('../views/find/micircle/MicircleView.vue'),
      redirect:'/micircle/rec_list',
      children: [
        { path: 'rec_list', name: 'rec_list', component: () => import('../views/find/micircle/micircleItem/MiRecList.vue') },
        { path: 'buy_list', name: 'buy_list', component: () => import('../views/find/micircle/micircleItem/MiBuyShowList.vue') },
        { path: 'question_list', name: 'question_list', component: () => import('../views/find/micircle/micircleItem/MiQuestionList.vue') },
        { path: 'evaluate_list', name: 'evaluate_list', component: () => import('../views/find/micircle/micircleItem/MiCeList.vue') },
        { path: '/micircle', redirect: '/micircle/rec_list' },
      ]
    },
    { path: '/', redirect: '/home' },
    { path: "/look:id", name: 'look', component: () => import('../views/home/LookMore.vue') },
    { path: "/info:id", name: 'info', component: () => import('../views/info/InfoView.vue') },
    { path: '/setting', name: 'setting', component: () => import('../views/user/UserBox/SettingView.vue') },
    { path: '/address', name: 'address', component: () => import('../views/user/UserBox/AddressView.vue') },
    { path: '/newaddress', name: 'newaddress', component: () => import('../views/user/UserBox/NewAddressView.vue') },
    //dashuaibi
    { path: '/search', name: 'search', component: () => import('../views/search/SearchView.vue') },
    { path: '/order', name: 'order', component: () => import('../views/user/OrderView.vue') },
    { path: '/kefu', name: 'kefu', component: () => import('../views/kefu/KeFu.vue') },
    { path: '/orders', name: 'orders', component: () => import('../views/order/OrdersView.vue') },



    
    { path: '/micircle/personal', name: 'personal', component: () => import('../views/find/micircle/PersonalView.vue') },

  ]
})

router.beforeEach((to) => {
  LoadingBar.start()
  setTimeout(() => {
    LoadingBar.finish()
  }, 500)
  const { name } = to
  let tokenObj = JSON.parse(localStorage.getItem("big-user") as string)
  // console.log(tokenObj)

  if (name != 'login') {
    if (tokenObj) {
      if (tokenObj.token) {
        return true
      } else {
        router.replace({
          name: 'login'
        })
      }
    } else {
      router.replace({
        name: 'login'
      })
    }
  } else {
    if (tokenObj) {
      if (tokenObj.token) {
        return false
      } else {
        return true
      }
    } else {
      return true
    }
  }


})


export default router
